import config from "config";

/**
 * 完整图片预览路径
 * @param _url
 * @returns {*|string}
 */
export function getListImageUrl(_url) {
    const baseUrl = config.baseUrl
    return _url ? _url.split(',').map(item => (baseUrl + ("/sys/common/static/" + item).replace("//", "/")))[0] : ""
}

export function getListImageUrlList(_url) {
    const baseUrl = config.baseUrl
    return _url ? _url.split(',').map(item => (
        {
            url: baseUrl + "/sys/common/static/" + item
        })) : []
}

/**
 * 打电话
 * @constructor
 */
export function PhoneCall(phone) {
    console.log("phone", phone)
    uni.makePhoneCall({
        phoneNumber: phone
    })
}

export const domHelper = {
    calcListHeight(refComponent, selector) {
        return new Promise((resolve, reject) => {
            if (!refComponent || typeof refComponent !== 'object') {
                return reject(new Error('无效的组件引用'))
            }
            const query = uni.createSelectorQuery().in(refComponent)
            query.select(selector).boundingClientRect(res => {
                if (!res || res.height === null) {
                    console.error('元素未找到:', selector)
                    return reject(new Error(`元素 ${selector} 未找到`))
                }

                const systemInfo = uni.getSystemInfoSync()
                resolve(`${(systemInfo.windowHeight - res.height) / 3}px`)
            }).exec()

            // 超时处理（解决安卓端渲染异常）
            setTimeout(() => {
                reject(new Error('DOM查询超时（2000ms）'))
            }, 2000)
        })
    }
}

